/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using VSLangProj80;
using EnvDTE;
using System.IO;
using DaveSexton.DocProject.Wizards.Foundation;
using DaveSexton.DocProject.Wizards;
using System.Text.RegularExpressions;

namespace DaveSexton.DocProject.Sandcastle
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
	public sealed class ChooseCompiledHelpWizardStep : WizardStep
	{
		#region Public Properties
		public override string Name
		{
			get
			{
				return Resources.WizardText.ChooseCompiledHelpStepName;
			}
		}

		public override bool Visible
		{
			get
			{
				return true;
			}
		}

		public override bool CanPreloadUserInterface
		{
			get
			{
				return true;
			}
		}

		public bool ProduceHelpVersion1
		{
			get
			{
				return Control.ProduceHelpVersion1;
			}
		}

		public bool ProduceHelpVersion2
		{
			get
			{
				return Control.ProduceHelpVersion2;
			}
		}
		#endregion

		#region Private / Protected
		private ChooseCompiledHelpControl Control
		{
			get
			{
				if (control == null)
					control = new ChooseCompiledHelpControl();

				IDocProject importFrom = (presentationStep.Presentation == null) ? null : presentationStep.Presentation.ImportFrom;

				if (lastImportFromProject != importFrom)
				{
					lastImportFromProject = importFrom;

					control.SetDefaultValues(lastImportFromProject);
				}

				return control;
			}
		}

		private ChooseCompiledHelpControl control;
		private readonly IDocProject project;
		private readonly ChoosePresentationWizardStep presentationStep;
		private IDocProject lastImportFromProject;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ChooseCompiledHelpWizardStep" /> class.
		/// </summary>
		public ChooseCompiledHelpWizardStep(IDocProject project, ChoosePresentationWizardStep presentationStep)
		{
			if (project == null)
				throw new ArgumentNullException("project");

			if (presentationStep == null)
				throw new ArgumentNullException("presentationStep");

			this.project = project;
			this.presentationStep = presentationStep;
		}
		#endregion

		#region Methods
		protected override object GetUserInterface(WizardController controller, bool preloading)
		{
			return Control;
		}

		protected override bool CanNavigateAway(WizardController controller, WizardStep toStep, bool forward)
		{
			return true;
		}

		protected override void WizardFinished(WizardController controller)
		{
			SandcastleProjectOptions options = (SandcastleProjectOptions) project.Options;

			// NOTE: Do not use the control property here otherwise the values will 
			// be reset since the presentation sets its ImportFrom property to null
			// after the import is completed, which will cause the default values to
			// be reassigned to this step's control in the Control property.
			if (control != null)
			{
				options.Help1xProject.OutputEnabled = control.ProduceHelpVersion1;
				options.Help2xProject.OutputEnabled = control.ProduceHelpVersion2;
			}
		}

		protected override void WizardCanceled(WizardController controller)
		{
			// do nothing
		}
		#endregion
	}
}
